package com.mooc.user.controller;

import com.mooc.user.common.PageParams;
import com.mooc.user.common.RestResponse;
import com.mooc.user.model.Agency;
import com.mooc.user.model.ListResponse;
import com.mooc.user.model.User;
import com.mooc.user.model.UserMsg;
import com.mooc.user.service.AgencyService;
import com.mooc.user.service.MailService;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("agency")
public class AgencyController {


     @Autowired
     private AgencyService agencyService;

     @Autowired
     private MailService mailService;

     @RequestMapping("agentList")
     public RestResponse<ListResponse<User>> agentList(@RequestBody  PageParams pageParams){

         Pair<List<User>, Long> pair = agencyService.getAllAgent(pageParams);
         ListResponse<User> response = ListResponse.build(pair.getKey(), pair.getValue());
         return  RestResponse.success(response);

     }


     @RequestMapping("agentDetail")
    public RestResponse<User> agentDtail(Long id){
          User  user = agencyService.getAgentDetail(id);
          return RestResponse.success(user);
     }


     @RequestMapping("list")
     public RestResponse<List<Agency>> agencyList(){
         try {
             Thread.sleep(1000*5);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         Agency agency = new Agency();
         List<Agency> list = agencyService.list(agency);
         return RestResponse.success(list);
     }

     @RequestMapping("sendUserMail")
    public  RestResponse sendUserMail(@RequestBody UserMsg userMsg){

         User agent = agencyService.getAgentDetail(userMsg.getAgentId());
         mailService.sendMail("来自用户"+userMsg.getEmail()+"的留言", userMsg.getMsg(), agent.getEmail());
         return RestResponse.success();
     }



}
